﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace AlphaSolutionsGP
{
    public class Staff : Person
    {
        MySqlConnection connection = new MySqlConnection("server=194.81.104.22;" + "User Id=alpha;" + "database=alpha_db;" + "password=bravo;");

        #region properties
        private string _staffID;
        private string _role;
        private string _dateEmployedFrom;
        private string _dateEmployedTo;
        private string _status;
        private string _reasonForDormancy;
        private string _password;
        #endregion

        #region constructors
        public Staff()
        {

        }

        //default constructor that accepts all fields as a parameter
        public Staff(string ID, string firstname, string middlename, string surname, string dob, string nationality, string hometown, string streetaddress, string postcode, string city
            , string mobilenumber, string telephonenumber, string emailaddress, string role, string date_employed_from, string date_employed_to, string status
            , string reason_for_Dormancy, string password)
            : base(firstname, middlename, surname, dob, nationality, hometown, streetaddress, postcode, city, mobilenumber, telephonenumber, emailaddress, "")
        {
            this._staffID = ID;            
            this._role = role;
            this._dateEmployedFrom = date_employed_from;
            this._dateEmployedTo = date_employed_to;
            this._status = status;
            this._reasonForDormancy = reason_for_Dormancy;
            this._password = password;
        }
        #endregion

        #region methods

        /*public void CopyStaff(Staff s)
            : base(Person p)
        {
            this._staffID = s.StaffID;
            this._firstName = s.FirstName;
            this._surname = s.Surname;
            this._DOB = s.DOB;
            this._nationality = s.Nationality;
            this._homeTown = s.HomeTown;
            this._streetAddress = s.StreetAddress;
            this._postcode = s.Postcode;
            this._city = s.City;
            this._mobileNumber = s.MobileNumber;
            this._telephoneNumber = s.TelephoneNumber;
            this._emailAddress = s.EmailAddress;
            this._role = s.Role;
            this._dateEmployedFrom = s.DateEmployedFrom;
            this._dateEmployedTo = s.DateEmployedTo;
            this._status = s.Status;
            this._reasonForDormancy = s.ReasonForDormancy;
        
        
        }*/

        public void createStaff()
        {
            try
            {
                connection.Open();
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Connection failed");
            }

            MySqlCommand cmd = connection.CreateCommand();
            Person p = new Person();
            cmd.CommandText = "INSERT INTO STAFF VALUES ('" + this._staffID + "','" + this.FirstName + "','" + this.MiddleName + "','" + this.Surname + "','" + convertToMySQLDate(this.DOB) + "','" + this.Nationality + "','" + this.HomeTown + "','" + this.StreetAddress + "','" + this.PostCode + "','" + this.City + "','" + this.MobileNumber + "','" + this.TelephoneNumber + "','" + this.AssignedEmail + "','" + this._role + "','" + convertToMySQLDate(this._dateEmployedFrom) + "','" + convertToMySQLDate(this._dateEmployedTo) + "','" + this.Status + "','" + this.ReasonForDormancy + "','" + this._password + "')";
            MySqlDataReader run = cmd.ExecuteReader();
            connection.Close();
        }

        public void amendStaff()
        {
            try
            {
                connection.Open();
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Connection failed");
            }
            MySqlCommand cmd = connection.CreateCommand();
            cmd.CommandText = "UPDATE STAFF SET name='" + FirstName + "', middle_name='" + MiddleName + "', surname='" + Surname + "', DOB='" + convertToMySQLDate(this.DOB) + "', nationality='" + Nationality + "', home_town='" + HomeTown + "', street_address='" + StreetAddress + "', postcode='" + PostCode + "', city='" + City + "', mobile_number='" + MobileNumber + "', telephone_number='" + TelephoneNumber + "', email_address='" + EmailAddress + "', role='" + _role + "', date_employment_from='" + convertToMySQLDate(this._dateEmployedFrom) + "', date_employment_to='" + convertToMySQLDate(this._dateEmployedTo) + "',  status='" + Status + "', reason_dormancy='" + ReasonForDormancy + "', password='" + _password + "' WHERE staff_ID=" + StaffID;
            MySqlDataReader run = cmd.ExecuteReader();
            connection.Close();
        }

        public void deleteStaff()
        {
            try
            {
                connection.Open();
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Connection failed");
            }
            MySqlCommand cmd = connection.CreateCommand();
            cmd.CommandText = "DELETE FROM STAFF WHERE staff_ID='" + this._staffID + "'";
            MySqlDataReader run = cmd.ExecuteReader();
            connection.Close();
        }

        //this method takes a string date as argument and convert it to mysql format date
        //used to convert the date when it comes to do INSERT into tables
        public static string convertToMySQLDate(string date)
        {
            //string dateToString = Convert.ToString(date);
            DateTime toDate = DateTime.Parse(date);
            string finalDate = toDate.Date.ToString("yyyy/MM/dd");

            return finalDate;
        }
        
        #endregion

        #region getters and setters

        public string ReasonForDormancy
        {
            get { return _reasonForDormancy; }
            set { _reasonForDormancy = value; }
        }
        

        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }
        

        public string DateEmployedTo
        {
            get { return _dateEmployedTo; }
            set { _dateEmployedTo = value; }
        }
        

        public string DateEmployedFrom
        {
            get { return _dateEmployedFrom; }
            set { _dateEmployedFrom = value; }
        }

        
        public string Role
        {
            get { return _role; }
            set { _role = value; }
        }


        public string StaffID
        {
            get { return _staffID; }
            set { _staffID = value; }
        }


        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }


        #endregion
    }
}
